﻿using System;
using System.Collections.Generic;
////using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using log4net;
using log4net.Config;
using RollBack.src.ui;
using System.IO;
using log4net.Repository.Hierarchy;
using RollBack.src;
using RollBackLib.src.utils;
using RollBackLib.src.core;
using RollbackLib.src.utils;

namespace RollBack
{
    public static class Program
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(Program));

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                Application.EnableVisualStyles();
                //Application.DoEvents();
                //Application.SetCompatibleTextRenderingDefault(false);
                //System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
                configureLogger(Path.GetDirectoryName(Application.ExecutablePath));
                String line = "";
                foreach (string str in Environment.GetCommandLineArgs())
                {
                    line = line + " " + str;
                }
                
                log.Info("Program arguments are <" + line + ">");
                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                BackupAppForm app = new BackupAppForm();
                AppUI.m_MainApp = (app);
                Application.Run(app);
                log.Info("------- Shutting down Backup application\n");
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Caught an exception " + e.Message);
                if (log != null)
                {
                    log.Fatal("Caught an exception ", e);
                    AppUI.ShowException(e.Message,e);
                    System.Environment.Exit(-1);
                }
            }
        }

        /// <summary>
        /// Gets called when there is a UI thread exception
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            log.Warn("Got a UI Thread exception", e.Exception);
            AppUI.ShowException("Got a UI error", e.Exception);
        }

        /// <summary>
        /// Configure the log4net logger
        /// </summary>
        public static void configureLogger(string exeParentDir)
        {
            string[] paths = {
                                 "./Resources/", 
                                 "../../Resources/",
                                 exeParentDir + "./Resources/",
                                 exeParentDir + "/../Resources",
                                 exeParentDir + "/../../Resources",
                             };
            RollbackLib.src.utils.Logger.configureLogger(paths);
            // Set up a simple configuration that logs on the console.
            log.Info("------- Starting Rollback. " +
                " Version=" + AppUtils.getApplicationVersion() +
                " Build=" + SVNVersionInfo.Revision +
                " AppDataPath=" + Application.UserAppDataPath +
                ""
                );

            /*
            if (log4netFileInfo != null)
            {
                log.Info("Loaded log4net xml file from " + log4netFileInfo.FullName);
            }
             */
        }
    }
}
